import { Room } from '../api/Room';
import { logger } from '../utils/logger';

export interface IGameEndCondition {
	id: string;
	func: (room: Room) => string | null;
}

export namespace GameEndRegistry {
	var conditions: IGameEndCondition[] = [];

	export function register(id: string, func: (room: Room) => string | null) {
		conditions.push({ id, func });
		logger.info(`RegisterGameEnd: ${id}`);
	}

	export function checkAll(room: Room): string | null {
		for (const condition of conditions) {
			const result = condition.func(room);
			if (result) return result;
		}
		return null;
	}
}

GameEndRegistry.register('dept', (room: Room) => {
	for (const i in room.depts) {
		const dept = room.depts[i];
		if (dept.power > 99) {
			return `${dept.name} 获得了游戏胜利`;
		}
	}
	return null;
});
